<?php
$defaultSiteId = Yii::app()->cmsManager->site['id'];
$script = "
var page_ids = [];
jQuery('#content').delegate('.actions-column .duplicate', 'click', function(){
    var source_page_id = parseInt(jQuery(this).attr('href').replace('#', ''));
    page_ids = [source_page_id];
    
    var title = jQuery(this).closest('td').prevAll('.sort-handle').text();
    jQuery('#duplicate-form .title').html('Duplicate the page \"<strong>'+title+'</strong>\"');
    jQuery('#duplicate-form #page_title').val('Copy '+title);
    
    jQuery.fancybox({
        'autoDimensions' : true,
        'width' : 500,
        'height' : 'auto',
        'padding' : 0,
        'margin' : 0,
        'type' : 'inline',
        'href' : '#duplicate-form',
        'scrolling': 'no',
        'modal': true,
        'titleShow': false
    });
    return false;
});
jQuery('#duplicate-form .duplicate').live('click', function(){
    var _this = this;
    var page_title = jQuery('#duplicate-form #page_title').val();
    var target_site_id = [{$defaultSiteId}];
    
    //validate page alias
    jQuery.get('".$this->createUrl('/Core/service/ajax', array('SID'=>'Cms.page.isExistAlias'))."', {'site_id': target_site_id, 'title' : page_title}, function(res){
        res = eval(res);
        if (res.exist != undefined && !res.exist)
        {
            var url = '".$this->createUrl('/Core/service/ajax', array('SID'=>'Cms.page.copy'))."';
            
            jQuery.fancybox.close();
            var data = {
                'target_site_id' : target_site_id,
                'page_id' : page_ids,
                'page_title' : page_title
            };
            jQuery.get(url, data, function(){
                alert('Duplicate successful.');
                window.location = window.location;
            });
        }
        else
            alert('Page \"'+page_title+'\" is exist.');
    });
    return false;
});
jQuery('#duplicate-form .cancel').live('click', function(){
    jQuery.fancybox.close();
    return false;
});
";
Yii::app()->clientScript->registerScript(__CLASS__.'#DuplicatePage', $script, CClientScript::POS_READY);
Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.pack.js');
Yii::app()->clientScript->registerCssFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.css');
?>
<div style="display: none;">
    <div id="duplicate-form" style="width: 400px;">
        <div class="title">Title</div>
        <div class="popup-content">
            <div class="sites row">
                <label>Page Title:</label>
            </div>
            <div class="sites row">                
                <?php echo CHtml::textField('page_title', '', array('style'=>'width:386px;'));?>
                <?php echo CHtml::hiddenField('source_page_id', '');?>
            </div>
            <div class="buttons">
                <?php echo CHtml::button('Cancel', array('class'=>'cancel'));?>
                <?php echo CHtml::button('Duplicate', array('class'=>'duplicate'));?>
            </div>
        </div>
    </div>
</div>
